#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include <mmsystem.h> 
#pragma comment(lib,"Winmm.lib")

#define high 864
#define width 591

IMAGE  img_bk,img_bullet1, img_bullet2, img_en1, img_en2, img_plane1, img_plane2, img_explode1, img_explode2; //瀹氫箟鑳屾櫙锛屽瓙寮癸紝鏁屾満锛岄鏈猴紝鐖嗙偢
int plane_x, plane_y;//椋炴満鍧愭爣
int bull_x, bull_y;//瀛愬脊鍧愭爣
int en_x, en_y;//鏁屾満鍧愭爣
int jieshu=0;  //缁撴潫
//int score=0;//得分
//char s[]="score:";
//寰楀埌鍙橀噺

void startup()
{
	mciSendString("open D:\\game_music.mp3 alias bkmusic", NULL, 0, NULL);  //鑳屾櫙闊充箰
	mciSendString("play bkmusic repeat", NULL, 0, NULL);  //寰幆鎾斁

	initgraph(width, high);
	loadimage(&img_bk, _T("D:\\background2.jpg"));  //591*864
	loadimage(&img_bullet1, _T("D:\\bullet1.jpg"));  //19*24
	loadimage(&img_bullet2, _T("D:\\bullet2.jpg"));  //19*24
	loadimage(&img_en1, _T("D:\\enemyPlane1.jpg"));  //104*148
	loadimage(&img_en2, _T("D:\\enemyPlane2.jpg"));  //104*148
	loadimage(&img_plane1, _T("D:\\planeNormal_1.jpg"));  //117*120
	loadimage(&img_plane2, _T("D:\\planeNormal_2.jpg"));  //117*120
	loadimage(&img_explode1, _T("D:\\planeExplode_1.jpg"));  //117*120
	loadimage(&img_explode2, _T("D:\\planeExplode_2.jpg"));  //117*120
	plane_x = width / 2;
	plane_y = high/2;
	bull_x = -100;
	bull_y = -100;
	en_x = width / 3;
	en_y = -148;
	jieshu = 0;
	
	
	
	BeginBatchDraw();  //寮€鐢?


}  //鏁版嵁鍒濆鍖?


void show()
{
	
		putimage(0, 0, &img_bk);  //鑳屾櫙
		
			putimage(plane_x - 50, plane_y - 60, &img_plane1, NOTSRCERASE);  //椋炴満
			putimage(plane_x - 50, plane_y - 60, &img_plane2, SRCINVERT);
			putimage(bull_x, bull_y, &img_bullet1, NOTSRCERASE);  //瀛愬脊
			putimage(bull_x, bull_y, &img_bullet2, SRCINVERT);
			putimage(en_x, en_y, &img_en1, NOTSRCERASE);  //鏁屾満
			putimage(en_x, en_y, &img_en2, SRCINVERT); 
			//outtextxy(width*0.5,high*0.8,s);
			//sprintf(score, "%d", 1024);
			//outtextxy(width*0.6,high*0.8,score);
		 
		FlushBatchDraw();  //鎵归噺鍒跺浘

}  //鏄剧ず鍑芥暟

void upwhitInput()
{
	MOUSEMSG m;//璁板綍榧犳爣娑堟伅
	while (MouseHit())
	{		m = GetMouseMsg();
			if (m.uMsg == WM_MOUSEMOVE)
			{
				plane_x = m.x;
				plane_y = m.y;
			}  //榧犳爣纭畾椋炴満浣嶇疆

			if (m.uMsg == WM_LBUTTONDOWN)
			{
				bull_x = plane_x - 8;
				bull_y = plane_y - 78;
				mciSendString("close gunmusic", NULL, 0, NULL);  //先关闭前一次
				mciSendString("open D:\\f_gun.mp3 alias gunmusic", NULL, 0, NULL);  //子弹音乐
				mciSendString("play gunmusic", NULL, 0, NULL);  //仅播放一次
			}  //榧犳爣宸﹂敭鍙戝皠瀛愬脊
	}

}  //鏈夐敭鍏ユ洿鏂?

void upwhitoutInput()
{
	if (bull_y > -20)
	{
		bull_y -= 5;
		Sleep(5);
	}  //瀛愬脊涓婄Щ
	if (en_y < high)
	{
		en_y += 2;
		Sleep(10);
	}
	else
	{
		en_x = rand() %( width-104);
		en_y = -148;
	}  //鏁屾満涓嬬Щ

	if ((bull_x <= en_x + 104) && (bull_x >= en_x) && (bull_y<en_y + 148))
	{
		en_x = rand() % (width - 104);
		en_y = -148;
		bull_y = -20;

		mciSendString("close jimusic", NULL, 0, NULL);  //先关闭前一次
		mciSendString("open D:\\gotEnemy.mp3 alias jimusic", NULL, 0, NULL);  //击中音乐
		mciSendString("play jimusic", NULL, 0, NULL);  //仅播放一次
	}  //瀛愬脊鎵撻鏈?

	if ((plane_x <= en_x + 150) && (plane_x >= en_x - 58) && (plane_y <=en_y + 208)&&(plane_y>=en_y-60))
	{
		jieshu++;
		mciSendString("close exmusic", NULL, 0, NULL);  //先关闭前一次
		mciSendString("open D:\\explode.mp3 alias exmusic", NULL, 0, NULL);  //爆炸音乐
		mciSendString("play exmusic", NULL, 0, NULL);  //仅播放一次
		
	}


	
}  //鏃犻敭鍏ユ洿鏂?



void gameover()
{
	EndBatchDraw();
	putimage(0, 0, &img_bk);  //鑳屾櫙
	putimage(plane_x - 50, plane_y - 60, &img_explode1, NOTSRCERASE);  //椋炴満鐖嗙偢
	putimage(plane_x - 50, plane_y - 60, &img_explode2, SRCINVERT);
	system("pause");
	exit(0);

}

int main()
{
	startup();  //鏁版嵁鍒濆鍖?
	while (jieshu==0)
	{
		show();  //鏄剧ず
		upwhitoutInput();  //鏇存柊鏃犺緭鍏		
		upwhitInput();  //鏇存柊鏈夎緭鍏?		
	}
	gameover();  //娓告垙缁撴潫涓庡悗缁搷浣?
	return 0;
}